package com.monkey.web.dto.request.user;

import com.monkey.core.domain.organization.Organization;
import com.monkey.core.domain.organization.OrganizationRepository;
import com.monkey.core.domain.user.User;
import com.monkey.utils.SpringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * Created by silentwu on 2015/7/3.
 */
public class ModifyUserDto {

    @NotNull(message = "{user.id.not.null}")
    private Long id;

    @NotEmpty(message = "{account.not.null}")
    @Pattern(regexp = User.ACCOUNT_PATTERN, message = "{user.account.not.valid}")
    @Length(min = User.ACCOUNT_MIN_LENGTH, max = User.ACCOUNT_MAX_LENGTH, message = "{user.account.not.valid}")
    private String account;

//    @NotEmpty(message = "{password.not.null}")
//    @Length(min = User.PASSWORD_MIN_LENGTH, max = User.PASSWORD_MAX_LENGTH, message = "{user.password.not.valid}")
//    private String password;

    @NotEmpty(message = "{nick.not.null}")
    @Length(min = User.NICK_MIN_LENGTH, max = User.NICK_MAX_LENGTH, message = "{user.nick.not.valid}")
    private String nick;

    @NotEmpty(message = "{email.not.null}")
    @Pattern(regexp = User.EMAIL_PATTERN, message = "{user.email.not.valid}")
    private String email;

    @NotEmpty(message = "{mobile.not.null}")
    @Pattern(regexp = User.MOBILE_PATTERN, message = "{user.mobile.not.valid}")
    private String mobile;

    @NotNull(message = "{organization.not.null}")
    private Long organizationId;

    public ModifyUserDto() {
    }

    //===============================================================

    public boolean changedAccount(String oldAccount) {
        return !oldAccount.equals(this.account);
    }

    public boolean changedNick(String oldNick) {
        return !oldNick.equals(this.nick);
    }

    public boolean changedEmail(String oldEmail) {
        return !oldEmail.equals(this.email);
    }

    public boolean changedMobile(String oldMobile) {
        return !oldMobile.equals(this.mobile);
    }

    public boolean changeOrganization(Long OldOrganizationId) {
        return !OldOrganizationId.equals(this.organizationId);
    }


    public boolean modifyUser(User user) {
        boolean modify = false;
        if (changedAccount(user.getAccount())) {
            user.setAccount(getAccount());
            modify = true;
        }
        if (changedEmail(user.getEmail())) {
            user.setEmail(getEmail());
            modify = true;
        }
        if (changedMobile(user.getMobile())) {
            user.setMobile(getMobile());
            modify = true;
        }
        if (changedNick(user.getNick())) {
            user.setNick(getNick());
            modify = true;
        }
        if (changeOrganization(user.getOrganization().getId())) {
            OrganizationRepository organizationRepository = SpringUtils.getBean(OrganizationRepository.class);
            Organization organization = organizationRepository.findOne(this.organizationId);
            user.setOrganization(organization);
            modify = true;
        }
        return modify;
    }
    //===============================================================


    public Long getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(Long organizationId) {
        this.organizationId = organizationId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

}
